package cn.woo5.test.detector.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

    public static String fetchGroup1(String src, String regex) {
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(src);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    public static List<String> fetchGroup1List(String src, String regex) {
        List<String> resultList = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(src);
        while (matcher.find()) {
            String group1 = matcher.group(1);
            resultList.add(group1);
        }
        return resultList;
    }

    public static String fetchOneNamedGroup(String src, String regex, String groupName) {
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(src);
        if (matcher.find()) {
            return matcher.group(groupName);
        }
        return null;
    }

    public static List<Map<String, String>> fetchNamedGroupList(String src, String regex, String... groupNames) {
        List<Map<String, String>> result = new ArrayList<>();

        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(src);

        while (matcher.find()) {
            Map<String, String> map = new HashMap<>();
            for (String groupName : groupNames) {
                map.put(groupName, matcher.group(groupName));
            }
            result.add(map);
        }
        return result;
    }

    public static Map<String, String> fetchNamedGroup(String src, String regex, String... groupNames) {
        Map<String, String> result = new HashMap<>();
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(src);
        if (matcher.find()) {
            for (String groupName : groupNames) {
                result.put(groupName, matcher.group(groupName));
            }
        }
        return result;
    }

    public static Map<String, String> fetchNameMap(String src, String regex) {
        Map<String, String> result = new HashMap<>();

        //Pattern.DOTALL表示.*可以匹配任何内容，
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(src);
        while (matcher.find()) {
            result.put(matcher.group(1), matcher.group(2));
        }
        return result;
    }


    public static List<String> fetchNameList(String src, String regex) {
        List<String> result = new ArrayList<>();

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(src);
        while (matcher.find()) {
            result.add(matcher.group(1));
        }
        return result;
    }


    public static void main(String[] args) {
        System.out.println(fetchGroup1("sjkfsafdk\n\t<input type=\"hidden\" name=\"CSRFToken\" value=\"e8272af1-282a-4a8d-87b2-3c3a8d43f29c\" />\n\t", "name=\"CSRFToken\"\\s+value=\"(.*?)\""));
        System.out.println(fetchNamedGroup("\n   TEST 123", ".*(?<login>\\w+) (?<id>\\d+)", "login", "id"));
        System.out.println(fetchOneNamedGroup("TEST 123", "(?<login>\\w+) (?<id>\\d+)", "login"));
    }
}
